/*
 * @Author: duanguang
 * @Date: 2022-02-16 18:33:31
 * @LastEditTime: 2022-04-08 14:56:46
 * @LastEditors: duanguang
 * @Description: 
 * @FilePath: /legions-server/app/controller/gitlab.ts
 * 「扫去窗上的尘埃，才可以看到窗外的美景。」
 */
import ResponseModel from '@/model/responseModel';
import { Controller } from 'egg';
import { HttpClientResponse } from 'urllib';
export default class GitlabController extends Controller {
    /**
     * 授权登录
     */
    public async oauthTokenLogin() {
        const { ctx } = this;
        const client_id = 'e16bb7de23c2b2765673417078253fbb7daa4188d1d45a391132739b1dffb140'
        const { code,redirect_uri } = this.ctx.query;
        const token_result = await this.ctx.curl<HttpClientResponse<string>>('https://gitlab.hoolinks.com/oauth/token',{
            dataType: 'json',
            contentType: 'application/json',
            method: 'POST',
            data: {
                client_id,
                code,
                redirect_uri,
                grant_type: 'authorization_code',
                client_secret: '9b861f8ababa20df89a3ba1e275c12c7f87dc9aa53363eeaa672d75cdd9d7009',
            }
        });
        let result = {
            gitlab_token: token_result.data,
            gitlab_user: null as any,
            admin_user: null as any,
        }
        if (token_result.data && token_result.data['access_token']) {
            const user_result = await this.ctx.curl<HttpClientResponse<string>>('https://gitlab.hoolinks.com/api/v4/user',{
                dataType: 'json',
                method: 'GET',
                headers: {
                    'Authorization': `Bearer ${token_result.data['access_token']}`,
                }
            })
            result.gitlab_user = user_result.data
            if (result.gitlab_user && result.gitlab_user['username']) {
                const admin_result = await this.ctx.service.user.oauthLogin({
                    name: result.gitlab_user['username'],
                    email: result.gitlab_user['email'],
                    password: '',
                    gitlab_open_id:result.gitlab_user['id'],
                    grant_type: 'authorization'
                })
                if (admin_result.success) {
                    result.admin_user = admin_result.data
                }
            }
        }

        ctx.body = new ResponseModel({
            data: result,
            message: '调用成功',
            success: true,
        });
    }

    /** 查询gitlab 项目数据 */
    public async projects() {
        const { search } = this.ctx.query;
        const project_result = await this.ctx.curl<HttpClientResponse<string>>('https://gitlab.hoolinks.com/api/v4/projects',{
            dataType: 'json',
            method: 'GET',
            data: {
                search,
            },
            headers: {
                Authorization: this.ctx.request.get('Authorization')
            }
        });
        console.log(project_result.data,'project_result.data');
        this.ctx.body = new ResponseModel({
            data: project_result.data,
            message: '调用成功',
            success: true,
        });
    }
    public async create_projects() {
        const { ctx } = this;
        const { body } = ctx.request;
        const errors = this.app.validator.validate(
            {
                name: { type: 'string',required: true },
                path: { type: 'string',required: true },
                namespace_id: { type: 'number',required: true },
            },
            body,
        )
        if (errors) {
            this.ctx.body = new ResponseModel({
                success: false,
                data: errors,
                message: '参数错误',
            });
            return;
        }
        const project_result = await this.ctx.curl<HttpClientResponse<string>>('https://gitlab.hoolinks.com/api/v4/projects',{
            dataType: 'json',
            contentType: 'application/json',
            method: 'POST',
            data:body,
            headers: {
                Authorization: this.ctx.request.get('Authorization')
            }
        });
        this.ctx.body = new ResponseModel({
            data: project_result.data,
            message: '成功',
            success: true,
        });
    }
    /** 查询gitlab 分组数据 */
    public async groups() {
        const project_result = await this.ctx.curl<HttpClientResponse<string>>('https://gitlab.hoolinks.com/api/v4/groups',{
            dataType: 'json',
            method: 'GET',
            headers: {
                Authorization: this.ctx.request.get('Authorization')
            }
        });
        this.ctx.body = new ResponseModel({
            data: project_result.data,
            message: '调用成功',
            success: true,
        });
    }
}
